/// @tags: DP on Splay ODT
#include <cstdio>
#include <cstring>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef long long LL;

constexpr int maxn = 2e5 + 3;

int root, scnt;

struct Node {
  int ch[2], fa;
  int a, b, c, len;
  LL siz;
  LL self[4][4], f[4][4];
} tr[maxn];

inline int iden(int x) { return tr[tr[x].fa].ch[1] == x; }

inline void pushUp(int x) {
  tr[x].siz = tr[x].len + tr[tr[x].ch[0]].siz + tr[tr[x].ch[1]].siz;
  LL(&self)[4][4] = tr[x].self;
  memset(self, 0, sizeof(self));
  self[0][0] = self[3][3] = (LL)tr[x].a * tr[x].len;
  self[1][1] = (LL)tr[x].b * tr[x].len;
  self[2][2] = (LL)tr[x].c * tr[x].len;
  for (rint i = 0; i < 4; ++i) {
    for (rint j = 0; j < 4; ++j) {
      for (rint k = i; k <= j; ++k) {
        self[i][j] = std::max(self[i][j], self[k][k]);
      }
    }
  }
  LL(&f)[4][4] = tr[x].f, (&lf)[4][4] = tr[tr[x].ch[0]].f, (&rf)[4][4] = tr[tr[x].ch[1]].f;
  LL tmp[4][4] = {0};
  for (rint i = 0; i < 4; ++i) {
    for (rint j = 0; j < 4; ++j) {
      for (rint k = i; k <= j; ++k) {
        tmp[i][j] = std::max(tmp[i][j], lf[i][k] + self[k][j]);
      }
    }
  }
  memset(f, 0, sizeof(f));
  for (rint i = 0; i < 4; ++i) {
    for (rint j = 0; j < 4; ++j) {
      for (rint k = i; k <= j; ++k) {
        f[i][j] = std::max(f[i][j], tmp[i][k] + rf[k][j]);
      }
    }
  }
}

inline void connect(int x, int y, bool op) {
  if (x) tr[x].fa = y;
  if (y) tr[y].ch[op] = x;
}

inline void rotate(int x) {
  int fa = tr[x].fa, ffa = tr[fa].fa;
  bool n = iden(fa), m = iden(x);
  connect(tr[x].ch[m ^ 1], fa, m);
  connect(fa, x, m ^ 1);
  connect(x, ffa, n);
  pushUp(fa);
  pushUp(x);
}

inline void splay(int x) {
  for (int fa; (fa = tr[x].fa); rotate(x))
    if (tr[fa].fa) rotate(iden(fa) == iden(x) ? fa : x);
  root = x;
}

inline int find(LL p) {
  int cur = root;
  while (true) {
    if (p <= tr[tr[cur].ch[0]].siz) {
      cur = tr[cur].ch[0];
    } else if (p <= tr[tr[cur].ch[0]].siz + tr[cur].len) {
      splay(cur);
      return cur;
    } else {
      p -= tr[tr[cur].ch[0]].siz + tr[cur].len;
      cur = tr[cur].ch[1];
    }
  }
}

inline int newNode(int len, int a, int b, int c) {
  ++scnt;
  tr[scnt].a = a;
  tr[scnt].b = b;
  tr[scnt].c = c;
  tr[scnt].len = len;
  return scnt;
}

inline int main() {
  int n;
  cin >> n;
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    LL p;
    int a, b, c, x;
    cin >> p >> a >> b >> c >> x;
    int node;
    if (p) {
      node = find(p);
      LL l = p - tr[tr[node].ch[0]].siz, r = tr[node].len - l;
      if (l) {
        int tmp = newNode(l, tr[node].a, tr[node].b, tr[node].c);
        connect(tr[node].ch[0], tmp, 0);
        connect(tmp, node, 0);
        pushUp(tmp);
      }
      if (r) {
        int tmp = newNode(r, tr[node].a, tr[node].b, tr[node].c);
        connect(tr[node].ch[1], tmp, 1);
        connect(tmp, node, 1);
        pushUp(tmp);
      }
    } else {
      node = root = newNode(x, a, b, c);
    }
    tr[node].a = a, tr[node].b = b, tr[node].c = c, tr[node].len = x;
    pushUp(node);
    splay(node);
    LL tmp = tr[root].f[0][3];
    cout << tmp - ans << '\n';
    ans = tmp;
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("P3991 [BJOI2017] 喷式水战改.in", "r", stdin);
  freopen("P3991 [BJOI2017] 喷式水战改.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
